<!--
 * @description: 组境创作
 * @fileName: CreativeLens
 * @author: 17076
 * @date: 2025/2/20-上午11:01
 * @version: V1.0.0
-->

<template>
  <div>
    <lens-list v-if="mode === 'list'" :lens-data="lensData" :play-id="playId" />
    <lens-grid v-else :play-id="playId" />
  </div>
</template>

<script>
import LensList from './list/LensList.vue'
import LensGrid from './grid/LensGrid.vue'
import { mapActions, mapGetters } from 'vuex'
import { queryLensList, textToImagesDefault } from '@/api/lens'
export default {
  name: 'CreativeLens',
  components: { LensGrid, LensList },
  props: {
    // 剧本id
    playId: {
      type: String,
      default: '0'
    }
  },
  data() {
    return {
      // 定时器
      timer: null,
      // 要加载的组件
      componentStr: null,
      // // 计时
      // count: 0,
      // 镜头数据
      lensData: []
    }
  },
  computed: {
    ...mapGetters({
      // 当前模式
      mode: 'lens/mode',
      // 选中分镜数据
      selectLens: 'lens/selectLens',
      // 是否自动生成
      createLens: 'lens/createLens'
    })
  },
  mounted() {
    // 获取分镜列表
    this.fetchData()
    // 暂时去掉自动生成以及开启定时器刷新镜头列表
    // if (this.createLens === '1') {
    //   // 开启定时器刷新镜头列表
    //   this.startTimer()
    //   this.handleStart()
    // }
  },
  beforeDestroy() {
    clearInterval(this.timer)
    this.timer = null
  },
  methods: {
    ...mapActions({
      setSelectLens: 'lens/setSelectLens'
    }),
    // 开启定时器刷新
    startTimer() {
      if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
      this.timer = setInterval(() => {
        this.fetchData()
      }, 3000)
    },
    // 判断任务是否完成
    isFinishDone() {
      if (this.lensData.length === 0) return
      // 03 状态表示任务进行中 判断所有数据都不是03则表示任务完成
      const isFinish = this.lensData.every(item => item.lensImagesList.every(subItem => (subItem['taskStatus'] !== '03')))
      console.log('任务是否完成', isFinish)
      if (isFinish && this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
    },
    // 获取数据
    fetchData() {
      if (!this.playId) return
      queryLensList({ playId: this.playId }).then(res => {
        this.lensData = res.data
        if (this.lensData.length > 0 && !this.selectLens) {
          this.setSelectLens(this.lensData[0])
        }
        // // 判断是否完成
        // this.isFinishDone()
      }).catch(_ => {
        if (this.timer) {
          clearInterval(this.timer)
          this.timer = null
        }
      })
    },
    // 开启文生图
    async handleStart() {
      if (!this.playId) return
      await textToImagesDefault({ playId: this.playId })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
